// pages/ScenicArea/ScenicAreaGoodsList/index.js
const utils = require('../utils.js')
const common = require('../../../public.js')
const config = require('../../../config')
const isurl = require('../../../utils/utils.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isFyApp: config.isFyApp,
    active: 1,
    storeId: 0,
    moduleType: 1,
    cateIndex: -1,
    defaultItemList: [],
    sale_category_list: [],
    tabs: '',
    bottoms: 0, //适配机型
    isplay: false, //是否播放
    isUrl: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    this.data.storeId = options.id ? options.id : config.id
    this.setData({
      moduleType: options.moduleType ? options.moduleType : config.type
    }, () => {
      that.data.moduleType == 2 ? that.setData({
        tabs: 'venue'
      }, () => {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面的对象
        var url = currentPage.route //当前页面url
        console.log(url)
        var isplay = isurl.isUrl(url) //判断音频所处的页面  // true代表在这个场馆里面，

        if (isplay) {
          this.setData({
            isplay: true
          })
        }
        this.setData({
          isUrl: isplay
        })
      }) : that.setData({
        tabs: ''
      })
    })

    this.shopList();
    var phone = wx.getSystemInfoSync({})

    let iosphone = ["iPhone 5", 'iPhone 6/7/8', 'iPhone 6/7/8 Plus']
    if (phone.model.indexOf('iPhone') != -1) { //包含  苹果手机的前提下判断
      for (var i = 0; i < iosphone.length; i++) {
        if (phone.model == iosphone[i]) {
          this.setData({
            bottoms: 0
          })
          return
        } else if (phone.model !== iosphone[i]) {
          this.setData({
            bottoms: 63
          })
        }
      }
    }
  },

  shopList() {
    const that = this
    const tempJson = {
      storeId: this.data.storeId, // 415
    }
    common.post('/store/getStoreSalingItemList', tempJson, function (res) {
      if (res.status === 200) {
        console.log('啊啊啊-', res);
        that.setData({
          sale_category_list: res.data.storeCatList,
          defaultItemList: res.data.defaultItemList,
          // defaultItemList: [],
        })

      } else {

      }
    }, function () {
      console.log('嘻嘻嘻嘻哈哈哈');

    })
  },

  onChangeTabbar(e) { //切换底部导航
    const index = e.detail.index
    const url = e.detail.url
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },
  onChangeTabbarc(e) { //切换底部导航
    console.log(e, "切")
    const index = e.detail.index
    const url = e.detail.url
    if (index == 2 && this.data.tabs == 'venue') {
      this.setData({
        active: index
      })
      return
    }
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },
  goodsDetail(e) {
    e.currentTarget.dataset.item.type = this.data.moduleType
    common.gotoGoodsPage(e.currentTarget.dataset.item)
  },

  changeCategory(idx, index) {
    this.setData({
      isLoading: true
    })
    const that = this
    const tempJson = {
      cid: idx,
      storeId: that.data.storeId,
      token: that.data.token,
      userId: that.data.userInfo.id
    }
    common.post('/store/mainPageInfo/storeCid', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          defaultItemList: res.data.data,
        })
      } else {
        that.setData({
          defaultItemList: []
        })
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoading: false
      })
    })
  },
  categoryClick(e) {
    const idx = e.currentTarget.dataset.id
    const index = e.currentTarget.dataset.index;
    if (this.data.cateIndex === index) {
      return
    }
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token,
          openid: data.openid,
          cateIndex: index
        })
        that.changeCategory(idx)
      }
    }, true)
  },
  onShow() {
    if (this.data.moduleType == 2) {
      this.setData({
        tabs: 'venue',
        isplay: true
      })
    }
  }
})